// page/component/details/details.js
var uid = ''
Page({
  data: {
    goods: {
      id: 1,
      image: '',
      title: '',
      price: 0.01,
      stock: '有货',
      detail: '',
      parameter: '125g/个',
      service: '不支持退货'
    },
    num: 1,
    totalNum: 0,
    hasCarts: false,
    curIndex: 0,
    show: false,
    scaleCart: false,
    isColl: false
  },

  addCount() {
    let num = this.data.num;
    num++;
    this.setData({
      num: num
    })
  },
  addToColl() {
    var {
      aid
    } = wx.getStorageSync('detail')
    if(!this.data.isColl){
      wx.cloud.callFunction({
        name:"addColl",
        data:{
          uid:wx.getStorageSync('openid'),
          id:aid
        }
      }).then(res=>{
        wx.showToast({
          title: '已收藏',
          mask:true
        })
        this.setData({
          isColl:true
        })
      })
    }

  },
  addToCart() {
    console.log(this.uid)
    var {
      aid
    } = wx.getStorageSync('detail')
    wx.cloud.callFunction({
      name: "detailAddPro",
      data: {
        uid: this.uid,
        id: aid
      }
    }).then(res => {
      console.log(res) //res.result.data.length
      var len = res.result.data.length
      //如果长度等于0，此物品还没有加入购物车，否则已经加入购物车
      if (len == 0) { //若还没有加入购物车，则先找到此商品的id并对其执行add函数
        // console.log(uid)
        this.cartAdd(aid)
        wx.showToast({
          title: '加入成功',
          mask:true
        })
      } else {
        wx.showToast({
          title: '已在购物车',
          mask:true
        })
        // console.log(aid)
        // this.add(aid)//若已经加入购物车，则对商品数量进行增加
      }
    })
    // const self = this;
    // const num = this.data.num;
    // let total = this.data.totalNum;

    // self.setData({
    //   show: true
    // })
    // setTimeout( function() {
    //   self.setData({
    //     show: false,
    //     scaleCart : true
    //   })
    //   setTimeout( function() {
    //     self.setData({
    //       scaleCart: false,
    //       hasCarts : true,
    //       totalNum: num + total
    //     })
    //   }, 200)
    // }, 300)

  },
  cartAdd(aid) {
    console.log(this.uid)
    // console.log(this.data.uid)
    wx.cloud.callFunction({
      name: "cartAddPro",
      data: {
        uid: this.uid,
        id: aid
      }
    }).then(res => {
      console.log(res)
    })
  },
  add(aid) { //在点击“+”或者点击加入购物车判断购物车中有这个商品时调用
    // console.log(aid)
    wx.cloud.callFunction({
      name: "addPro",
      data: {
        uid: this.uid,
        id: aid, //商品的id
      }
    }).then(res => {
      console.log(res)
    })
  },

  bindTap(e) {
    const index = parseInt(e.currentTarget.dataset.index);
    this.setData({
      curIndex: index
    })
  },
  onShow() {

    var uid = wx.getStorageSync('openid')
    var {
      apic,
      aid,
      acontent,
      aname,
      aprice
    } = wx.getStorageSync('detail')
    //console.log(uid)
    this.uid = uid
    this.setData({
      goods: {
        image: apic,
        title: aname,
        detail: acontent,
        price: aprice
      },
    })
    wx.cloud.callFunction({
      name: "detailAddColl",
      data: {
        uid: this.uid,
        id: aid
      }
    }).then(res => {
      console.log(res) //res.result.data.length
      var len = res.result.data.length
      //如果长度等于0，此物品还没有加入收藏表，否则已经加入收藏表
      if (len == 0) { //若还没有加入购物车，则先找到此商品的id并对其执行add函数
        // console.log(uid)
        this.setData({
          isColl: false
        })
      } else {
        this.setData({
          isColl: true
        })
      }
    })
  }

})